import { Button, message, Radio } from 'antd';
import { connect } from 'dva';
import React, { PureComponent } from 'react';
import enums from '../enums';
import styles from './index.less';
import { filterAuth } from '@/support/common';

class LiftVehicleControl extends PureComponent {
  state = {
    isEdit: false,
    financingDue: undefined,
    afterWarning: undefined,
    saveBtnLoading: false,
  };

  componentDidMount() {
    this.handleGetRiskRemoveConfig();
  }

  handleGetRiskRemoveConfig = () => {
    this.props.dispatch({
      type: 'liftVehicleControl/postObjWithRes',
      payload: {
        url: '/mscreditbusiness/risk/remove/config/show',
      },
      callback: (response) => {
        if (response && response.statusCode == '0') {
          // if (response.data) {
          this.setState({
            financingDue: response.data?.financingDue,
            afterWarning: response.data?.afterWarning,
            isEdit: false,
          });
          // }
          // } else {
          //   message.error(response.statusMessage);
        }
      },
    });
  };

  handleSave = () => {
    const { financingDue, afterWarning } = this.state;

    if (!financingDue && financingDue != 0) {
      message.error('请选择解除授信状态类型');
      return;
    }

    if (!afterWarning && afterWarning != 0) {
      message.error('请选择恢复行驶状态类型');
      return;
    }

    this.setState({
      saveBtnLoading: true,
    });
    this.props.dispatch({
      type: 'liftVehicleControl/postObjWithRes',
      payload: {
        url: '/mscreditbusiness/risk/remove/config/save',
        financingDue,
        afterWarning,
      },
      callback: (response) => {
        if (response && response.statusCode == '0') {
          message.success('操作成功');
          this.handleGetRiskRemoveConfig();
          // } else {
          //   message.error(response.statusMessage);
        }
        this.setState({
          saveBtnLoading: false,
        });
      },
    });
  };

  render() {
    const { isEdit, financingDue, afterWarning, saveBtnLoading } = this.state;
    const {
      permissions,
      location: { pathname = '/' },
    } = this.props;
    return (
      <div className={styles.root}>
        <div className={styles.settingOne}>
          <div className={styles.title}>融资到期后,如果没有逾期</div>
          <Radio.Group
            value={financingDue}
            disabled={!isEdit}
            onChange={(e) => {
              this.setState({
                financingDue: e.target.value,
              });
            }}
          >
            <div className={styles.radioItem}>
              <Radio value={enums.financingDue.auto.value} style={{ fontSize: '16px' }}>
                自动解除授信状态
              </Radio>
            </div>
            <div className={styles.radioItem}>
              <Radio value={enums.financingDue.manual.value} style={{ fontSize: '16px' }}>
                手动解除授信状态
              </Radio>
            </div>
          </Radio.Group>
        </div>
        <div className={styles.settingOne}>
          <div className={styles.title}>车辆发生预警后,如果预警状态解除</div>
          <Radio.Group
            value={afterWarning}
            disabled={!isEdit}
            onChange={(e) => {
              this.setState({
                afterWarning: e.target.value,
              });
            }}
          >
            <div className={styles.radioItem}>
              <Radio value={enums.afterWarning.auto.value} style={{ fontSize: '16px' }}>
                自动解除预警状态并恢复至正常行驶状态
              </Radio>
            </div>
            <div className={styles.radioItem}>
              <Radio value={enums.afterWarning.manual.value} style={{ fontSize: '16px' }}>
                自动解除预警状态,手动恢复至正常行驶状态
              </Radio>
            </div>
          </Radio.Group>
        </div>
        <div className={styles.btn}>
          {isEdit ? (
            <>
              <Button
                loading={saveBtnLoading}
                type="primary"
                size="small"
                style={{ marginRight: '20px' }}
                onClick={() => this.handleSave()}
              >
                保存
              </Button>
              <Button
                size="small"
                onClick={() => {
                  this.setState({
                    isEdit: false,
                  });
                  this.handleGetRiskRemoveConfig();
                }}
              >
                取消
              </Button>
            </>
          ) : filterAuth('liftVehicleControl-edit', permissions, pathname) ? (
            <Button
              type="primary"
              size="small"
              onClick={() => {
                this.setState({
                  isEdit: true,
                });
              }}
            >
              修改
            </Button>
          ) : null}
        </div>
      </div>
    );
  }
}
export default connect((state) => {
  return {
    loading: state.loading.effects['liftVehicleControl/postObjWithRes'],
    permissions: state?.menu?.permissions,
    liftVehicleControl: state.liftVehicleControl,
  };
})(LiftVehicleControl);
